﻿using System;
using System.Web.UI;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using System.Web.UI.WebControls;
using Legazpi.Payroll.DataAccess.DataObjects;
using Legazpi.Payroll.DataAccess.DataObjects.Enums;
using Web.Codes;
using Web.Utils;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class AdjustmentTable : System.Web.UI.Page
    {
        [Inject]
        public IAdjustmentService _adjustmentService { get; set; }

        public bool IsSuccess { get; set; }
        public string NotifyText { get; set; }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _adjustmentService.GetAll();
            repeater.DataBind();
        }

        void Delete(int id)
        {
            _adjustmentService.Delete(id);
            IsSuccess = _adjustmentService.Save();
            NotifyText = "Successfully Deleted.";
            BindGrid();
        }

        void Edit(int id, RepeaterItem item)
        {
            var adjustment = _adjustmentService.GetById(id);

            AccountType.ClearSelection();
            AccountSign.ClearSelection();

            ObjectToData(adjustment);
           
            SubmitBtn.Text = "Update";
        }
        void Update(int id)
        {
            var adjustment = _adjustmentService.GetById(id);
            if (adjustment == null)
            {
                Add();
                return;
            }
            DataToObject(ref adjustment);

            _adjustmentService.Update(adjustment);
            IsSuccess = _adjustmentService.Save();
            NotifyText = "Successfully Updated Record.";
            ClearForm();
            BindGrid();
        }
      
        void Add()
        {
            var adj = new Adjustment();

            DataToObject(ref adj);

            _adjustmentService.Add(adj);
            IsSuccess = _adjustmentService.Save();
            NotifyText = "Successfully Added New Record.";
            BindGrid();
        }
        void DataToObject(ref Adjustment obj)
        {
            obj.Code = CodeTxt.Text;            
            obj.Description = DescTxt.Text;
            obj.AccountType = int.Parse(AccountType.SelectedValue);
            obj.AccountSignEnum = (AccountSign)int.Parse(AccountSign.SelectedValue);
        }
        void ObjectToData(Adjustment obj)
        {
            AdjusmentId.Value = obj.Id.ToString();
            CodeTxt.Text = obj.Code;
            DescTxt.Text = obj.Description;
            
            var acntType = AccountType.Items.FindByValue(obj.AccountType.ToString());            
            acntType.Selected = acntType != null;

            var acntSign = AccountSign.Items.FindByValue(((int)obj.AccountSignEnum).ToString());            
            acntSign.Selected = acntSign != null;

        }
        void Save()
        {
            int Id;
            int.TryParse(AdjusmentId.Value, out Id);
            if (Id == 0)
                Add();
            else
                Update(Id);
        }
        void ClearForm()
        {
            CodeTxt.Text = string.Empty;
            AdjusmentId.Value = "0";
            DescTxt.Text = string.Empty;
            SubmitBtn.Text = "Submit";
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
                AccountType.ToAdjAccountTypeSelectList(1);
                AccountSign.ToAdjAccountSignSelectList(0);
            }                
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Update_Command:
                        Edit(intTemp, e.Item);
                        break;
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }

        protected void AddNewBtn_Click(object sender, EventArgs e)
        {
            Save();
            if (IsSuccess)
            {
                ClearForm();
                BindGrid();
            }
        }

        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            ClearForm();
        }
    }
}